/**
 * $err:function(type)
 * Style:
 * 		extendXNail
 * 		shrinkXNail
 */
XN.XNail = {
	init:function(_id,_w,_h){
		XN.XNail.Cot = document.createElement("div");
		XN.XNail.Cot.className = "IM";
		document.body.appendChild(XN.XNail.Cot);
		XN.XNail.w = _w?_w:0;
		XN.XNail.h = _h?_h:0;
		if(FWK.isIE){
			XN.XNail.Cot.innerHTML = '<object width="0px" height="0px" id="'+_id+'" CLASSID="CLSID:9B5CD03D-07B7-4134-9A87-3074C5F8CBA0"><param name="transparent" value="wmode" codebase="http://www.'+XN.doMain+'/IE3DWindowControl.ocx"></param></object>';
		}
		else{
			XN.XNail.Cot.innerHTML = '<embed width="0px" height="0px" id="'+_id+'" type="application/mozilla-npruntime-scriptable-plugin" pluginspage="http://www.'+XN.doMain+'/npFF3DWindowControl.dll">';
		}
		XN.XNail.X = XN.XNail.Cot.firstChild;
		return XN.XNail.X;
	},
	onInited:function(num,url){
		num = parseInt(num);
		switch(num){
			case 0:
				XN.XNail.$info(0,"客户端登录成功");
				break;
			case 1:
				XN.XNail.$info(1,"登录客户端失败");
				break;
			case 2:
				XN.XNail.$info(2,"登录房间成功");
				break;
			case 3:
				XN.XNail.$info(3,"登录房间失败");
				break;
			case 7:
				//通知网页 离线
				XN.XNail.shrink();
				try{XN.Entry.logout();}catch(e){};	//试着通知网页离线
				XN.XNail.$info(7,"用户点击了离线");
				break;
			case 8:
				//展开房间
				XN.XNail.extend();
				XN.XNail.$info(8,"重新进房间,还是原来的房间");
				break;
			case 9:
				//收缩房间
				XN.XNail.shrink();
				XN.XNail.$info(9,"自然状态的离线情况");
				break;
			case 10:
				document.location.href=url;
				break;
			default:
				XN.XNail.$info(10,"没有预料的错误");
				break;
		}
	},
	//展开房间
	extend:function(){
		XN.XNail.X.className = "extendXNail";
		XN.XNail.X.setAttribute("width",XN.XNail.w+"px");
		XN.XNail.X.setAttribute("height",XN.XNail.h+"px");
	},
	//收缩房间
	shrink:function(){
		XN.XNail.X.className = "shrinkXNail";
		XN.XNail.X.setAttribute("width","0px");
		XN.XNail.X.setAttribute("height","0px");
	},
	onLine:function(){
		FWK.Util.AjaxList.open("/login/memberIdentity.do?m=refresh","POST",{},XN.XNail.onLineComplete);
	},
	onLineComplete:function(_xml,_url){
		XN.XNail.url = _url || XN.XNail.url || 0;
		var ret = (_xml)?XN.checkResponse(_xml):{code:-2,des:"无法获取身份"};
		if(ret==true){
			var str = XN.XNail.getVstr();
			if(str){
				XN.XNail.initClient(_xml.selectSingleNode("./member/userInfo/@account").nodeValue, str + "\0",XN.XNail.url);
			}
			else{
				XN.XNail.$info(-1,"客户端没有启用Cookie");
			}
		}
		else{
			XN.XNail.$info(ret.code,ret.des);
		}
		delete ret;
	},
	getVstr:function(){
		return XN.getCookieQuery("SNAIL-D1XN-IM");
	},
	isLogin:function(){
		if(XN.Entry.loginXML){
			return true
		}
		else{
			try{XN.XNail.$noLogin();}catch(e){}
			return false;
		}
	},
	$info:function(_code,_msg){
		alert(_code + "\n" + _msg);
	},
	$noClient:function(_msg){
		alert(_msg);
	},
	$err:function(_ret){
		alert(_ret.des);
	},
	//调用插件方法
	//打开房间
	enterRoom:function(_userId,_roomName){
		if(!XN.XNail.isLogin()){return ;}
		try{
			XN.XNail.X.enterRoom(_userId,_roomName);
		}
		catch(e){
			XN.XNail.$noClient("enterRoom");
		}
	},
	//添加朋友
	addFriend:function(_mID, _fID){
		if(_mID==_fID){
			XN.XNail.$err({
				des: "不能自己添加自己好友",
				code: 0
			});
			return ;
		}
		if(!XN.XNail.isLogin()){return ;}
		try{
			XN.XNail.X.addFriend(_mID, _fID);
		}
		catch(e){
			XN.XNail.$noClient("addFriend");
		}
	},
	//和他聊天
	doChat:function(_mID, _fID){
		if(_mID==_fID){
			XN.XNail.$err({
				des: "不能自己和自己聊天",
				code: 1
			});
			return ;
		}
		if(!XN.XNail.isLogin()){return ;}
		try{
			XN.XNail.X.doChat(_mID, _fID);
		}
		catch(e){
			XN.XNail.$noClient("doChat");
		}
	},
	//登录XNail
	initClient:function(_mID, _str,_url){
		if(!XN.XNail.isLogin()){return ;}
		try{
			XN.XNail.X.initClient(_mID, _str,_url);
		}
		catch(e){
			XN.XNail.$noClient("initClient");
		}
	},
	//关闭网页聊天窗口
	destroyWindow:function(){
		try{
			XN.XNail.X.destroyWindow();
		}
		catch(e){
			XN.XNail.$noClient("destroyWindow");
		}
	},
	//呼出客户端
	showClient:function(){
		if(!XN.XNail.isLogin()){return ;}
		try{
			XN.XNail.X.showClient();
		}
		catch(e){
			XN.XNail.$noClient("showClient");
		}
	}
};

window.onInited = XN.XNail.onInited;
window.onLine = XN.XNail.onLine;